<template>
  <div>
     <a-tabs :activeKey="first_tab" @change="activeKeyChange">
       <a-tab-pane key="1" tab="預定">
        <a-card :bordered="false" :bodyStyle="tstyle">
              <div class="table-page-search-wrapper">
                <a-form layout="inline">
                  <a-row :gutter="48">
                    <a-col :md="16" :sm="24">
                      <a-form-item label="預約房間">
                        <a-input v-model="queryParam.searchValue" allow-clear placeholder="請輸入房產名稱、房間名稱、預定人姓名"/>
                      </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24" >
                      <span class="table-page-search-submitButtons">
                        <a-button type="primary" @click="$refs.table.refresh(true)" >查詢</a-button>
                        <a-button style="margin-left: 8px" @click="() => queryParam = { type: 1 }">重置</a-button>
                      </span>
                    </a-col>
                  </a-row>
                </a-form>
              </div>
            </a-card>
            <a-card :bordered="false">
              <div>
                  <a-button type="primary" icon="plus" @click="$refs.addForm.add(1, '')">預定</a-button>
              </div>
              <s-table
                ref="table"
                size="default"
                :columns="columns"
                :data="loadData"
                :alert="true"
                :rowKey="(record) => record.id"
              >
                <span slot="collectionTypescopedSlots" slot-scope="text">
                  {{ costType(text) }}
                </span>
                <span slot="action" slot-scope="text, record">
                  <a @click="$refs.readOnlyForm.edit(record)">查看</a>
                  <a-divider type="vertical" />
                  <a-dropdown >
                    <a class="ant-dropdown-link">
                      更多 <a-icon type="down" />
                    </a>
                    <a-menu slot="overlay">
                      <a-menu-item>
                         <a @click="$refs.addLeaseForm.add(2, record)">簽約</a>
                      </a-menu-item>
                      <a-menu-item>
                        <a @click="$refs.cancelRoomReservation.edit(record.id)">取消預訂</a>
                      </a-menu-item>
                      <a-menu-item>
                         <a @click="$refs.printReservation.edit(record)">打印</a>
                      </a-menu-item>
                    </a-menu>
                  </a-dropdown>
                </span>
              </s-table>
            </a-card>
       </a-tab-pane>
       <a-tab-pane key="2" tab="歷史預定">
             <a-card :bordered="false" :bodyStyle="tstyle">
              <div class="table-page-search-wrapper">
                <a-form layout="inline">
                  <a-row :gutter="48">
                    <a-col :md="16" :sm="24">
                      <a-form-item label="預約房間">
                        <a-input v-model="queryParam2.searchValue" allow-clear placeholder="請輸入房產名稱、房間名稱、預定人姓名"/>
                      </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24" >
                      <span class="table-page-search-submitButtons">
                        <a-button type="primary" @click="$refs.table2.refresh(true)" >查詢</a-button>
                        <a-button style="margin-left: 8px" @click="() => queryParam2 = { type: 2 }">重置</a-button>
                      </span>
                    </a-col>
                  </a-row>
                </a-form>
              </div>
            </a-card>
             <a-card :bordered="false">
              <s-table
                ref="table2"
                size="default"
                :columns="columnsLs"
                :data="loadData2"
                :rowKey="(record) => record.id"
                :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
              >
               <template class="table-operator" slot="operator">
                   <a-popconfirm  placement="topRight" title="刪除預訂將同時刪除預訂產生的對應流水。刪除之後將不可恢復，確定要刪除嗎？" @confirm="() => delPl()">
                    <a class="danger">批量刪除</a>
                  </a-popconfirm>
                </template>
                <span slot="collectionTypescopedSlots" slot-scope="text">
                  {{ costType(text) }}
                </span>
                <span slot="status" slot-scope="text">
                  {{ statusType(text) }}
                </span>
                <span slot="action" slot-scope="text, record">
                  <a @click="$refs.readOnlyForm.edit(record)">查看</a>
                </span>
              </s-table>
            </a-card>
       </a-tab-pane>
     </a-tabs>
     <add-form ref="addForm" @ok="handleOk" />
     <add-lease-form ref="addLeaseForm" @ok="handleOk" />
     <edit-form ref="editForm" @ok="handleOk" />
     <read-only-form ref="readOnlyForm" @ok="handleOk"/>
     <cancel-room-reservation ref="cancelRoomReservation" @ok="handleOk"/>
     <print-reservation ref="printReservation"/>
  </div>
</template>
<script>
  import { STable } from '@/components'
  import { roomReservationPage, deleteAllSelect } from '@/api/modular/main/roomreservation/roomReservationManage'
  import { busMeansOfTransactionList } from '@/api/modular/main/busmeansoftransaction/busMeansOfTransactionManage'
  import addForm from './addForm.vue'
  import editForm from './editForm.vue'
  import readOnlyForm from './readOnlyForm.vue'
  import cancelRoomReservation from './cancelRoomReservation.vue'
  import printReservation from './printReservation.vue'
  import addLeaseForm from '../houselease/addForm.vue'
  export default {
    components: {
      STable,
      addForm,
      editForm,
      readOnlyForm,
      cancelRoomReservation,
      printReservation,
      addLeaseForm
    },
    data () {
      return {
        first_tab: '1',
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {
          type: 1
        },
        queryParam2: {
          type: 2
        },
        // 表头
        columns: [
          {
            title: '預定房間',
            align: 'center',
            dataIndex: 'roomCode'
          },
          {
            title: '預定人',
            align: 'center',
            dataIndex: 'reservation'
          },
          {
            title: '聯係方式',
            align: 'center',
            dataIndex: 'phone'
          },
          {
            title: '最晚入住日期',
            align: 'center',
            dataIndex: 'latestDate'
          },
          {
            title: '定金',
            align: 'center',
            dataIndex: 'deposit'
          },
          {
            title: '收款日期',
            align: 'center',
            dataIndex: 'collectionDate'
          },
          {
            title: '收款方式',
            align: 'center',
            dataIndex: 'collectionType',
            scopedSlots: { customRender: 'collectionTypescopedSlots' }
          },
          {
            title: '備注',
            align: 'center',
            dataIndex: 'remark'
          },
          {
            title: '操作',
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' }
          }
        ],
        columnsLs: [
          {
            title: '預定房間',
            align: 'center',
            dataIndex: 'roomCode'
          },
          {
            title: '預定人',
            align: 'center',
            dataIndex: 'reservation'
          },
          {
            title: '聯係方式',
            align: 'center',
            dataIndex: 'phone'
          },
          {
            title: '最晚入住日期',
            align: 'center',
            dataIndex: 'latestDate'
          },
          {
            title: '定金',
            align: 'center',
            dataIndex: 'deposit'
          },
          {
            title: '收款日期',
            align: 'center',
            dataIndex: 'collectionDate'
          },
          {
            title: '收款方式',
            align: 'center',
            dataIndex: 'collectionType',
            scopedSlots: { customRender: 'collectionTypescopedSlots' }
          },
          {
            title: '備注',
            align: 'center',
            dataIndex: 'remark'
          },
          {
            title: '狀態',
            align: 'center',
            dataIndex: 'status',
            scopedSlots: { customRender: 'status' }
          },
          {
            title: '操作',
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' }
          }
        ],
        collectionTypeData: [],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return roomReservationPage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        loadData2: parameter => {
          return roomReservationPage(Object.assign(parameter, this.queryParam2)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        statusTypeData: [ { id: 1, name: '已入住' }, { id: 2, name: '已取消' } ]
      }
    },
    created () {
      this.initNeedData()
    },
    methods: {
       costType (type) {
        const values = this.collectionTypeData.filter(item => item.id === type + '')
        if (values.length > 0) {
          return values[0].name
        }
      },
      statusType(type) {
        const values = this.statusTypeData.filter(item => item.id === type)
        if (values.length > 0) {
          return values[0].name
        }
      },
      initNeedData() {
        busMeansOfTransactionList().then((res) => {
          if (res.code === 200) {
            this.collectionTypeData = res.data
          } else {
            this.$message.error(res.msg)
          }
        })
      },
      // 第一步tab点击
      activeKeyChange(key) {
        this.first_tab = key
      },
      roomReservationDelete (record) {
        deleteAllSelect(record).then((res) => {
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
            this.$refs.table2.refresh()
          } else {
            this.$message.error('删除失败') // + res.message
          }
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      handleOk () {
        this.$refs.table.refresh()
        this.$refs.table2.refresh()
      },
      onSelectChange(selectedRowKeys) {
        this.selectedRowKeys = selectedRowKeys
      },
      delPl() {
        if (this.selectedRowKeys.length === 0) {
          this.$message.success('請選擇要刪除的信息')
          return
        }
        deleteAllSelect(this.selectedRowKeys).then((res) => {
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.$refs.table2.refresh()
          } else {
            this.$message.error(res.msg) // + res.message
          }
        })
        this.selectedRowKeys = []
      }
    }
  }
</script>
<style lang="less" scoped>
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
  .danger{
    color: red;
    border: red solid 1px;
    padding: 6px;
  }
</style>
